<?php
class M_student extends MY_Model
{
	function get_student($profile_id){
		$sq= 'SELECT enrollments.e_id as enrollment_id,
					 enrollments.edit_profile as edit,
					 profiles.*,
					 concat(employees.first_name," ",employees.last_name) as teacher_name,
					 concat(profiles.sibling_fname1," ",profiles.sibling_lname1) as sibling1,
					 concat(profiles.sibling_fname2," ",profiles.sibling_lname2) as sibling2,
					 concat(profiles.sibling_fname3," ",profiles.sibling_lname3) as sibling3,
					 concat(profiles.sibling_fname4," ",profiles.sibling_lname4) as sibling4,
					 concat(profiles.sibling_fname5," ",profiles.sibling_lname5) as sibling5,
					 concat(profiles.sibling_fname6," ",profiles.sibling_lname6) as sibling6,
					 enrollments.e_student_id as idno,
					 DATE_FORMAT(enrollments.e_created_at,"%M %e, %Y") as date_registered,
					 DATE_FORMAT(profiles.dob,"%M %e, %Y") as dob_formated,
					 concat(profiles.firstname," ",profiles.middlename," ",profiles.lastname) as fullname,
					 enrollments.e_grade_level as level_id,
					 enrollments.e_enrollee_type as student_type,
					 enrollments.e_section_id,
					 levels.level_desc,
					 levels.level_category,
					 sections.section_name
			  FROM enrollments
			  LEFT JOIN profiles ON profiles.profile_id = enrollments.e_profile_id
			  LEFT JOIN levels ON levels.level_id = enrollments.e_grade_level
			  LEFT JOIN sections ON sections.id = enrollments.e_section_id
			  LEFT JOIN employees ON sections.adviser_id = employees.emp_id
			  WHERE enrollments.e_id = ?
			  LIMIT 1; 
			  ';
		$q = $this->db->query($sq,array($profile_id));
		return $q->num_rows() >= 1 ? $q->row() : FALSE;
	}
	

	function get_student_user_id($profile_id){
		$q = $this->db->select('user_id')->where('profile_id', $profile_id)->get('profiles');
		return $q->num_rows() >= 1 ? $q->row()->user_id : FALSE;
	}
	
	function get_student_profile_id($id){
		$q = $this->db->select('profile_id')->where('user_id', $id)->get('profiles');
		return $q->num_rows() >= 1 ? $q->row()->profile_id : FALSE;
	}
	
	function get_student_data_by_user_id($id)
	{
		$sql = "SELECT p.profile_id,e.e_id
				FROM enrollments e
				LEFT JOIN profiles p ON p.profile_id = e.e_profile_id
				LEFT JOIN users u ON u.id = p.user_id
				WHERE u.id = ?
				AND e.e_ay_id = ?
				LIMIT 1";
		$query = $this->db->query($sql,array($id,$this->c_sy->id));
		return $query->num_rows() >=1 ? $query->row() : FALSE;
	}
	
	function get_enrollment_id_by_user_id($x)
	{
		$sql = "SELECT e_id,firstname,lastname,middlename FROM enrollments 
				LEFT JOIN profiles ON profiles.profile_id = enrollments.e_profile_id
				LEFT JOIN users ON users.id = profiles.user_id
				WHERE users.id = ? 
				AND enrollments.e_archived = 0
				AND enrollments.verified = 1
				AND users.userType = 'student'
				AND users.userStatus = 'active'
				AND enrollments.e_archived = 0
				AND enrollments.verified = 1
				AND enrollments.e_ay_id = ?
				LIMIT 1";
		$query = $this->db->query($sql,array($x,$this->c_sy->id));
		
		if($query->num_rows() >=1 )
		{
			return $query->row();
		}else{
			return FALSE;
		}
	}

	function _update_personal_profile_data($_pst,$e_id= '',$p_id = '')
	{
		$profile_data['email'] = $_pst->email;
		$profile_data['firstname'] = $_pst->child_fname;
		$profile_data['middlename'] = $_pst->child_mname;
		$profile_data['lastname'] = $_pst->child_lname;
		$profile_data['nickname'] = $_pst->child_nickname;
		$profile_data['name_ext'] = $_pst->child_name_ext;
		$profile_data['pob'] = $_pst->child_placeofbirth;
		$profile_data['dob'] = DateTime::createFromFormat('m/d/Y',$_pst->child_bdate)->format('Y-m-d');
		$profile_data['gender'] = $_pst->gender;
		$profile_data['religion'] = $_pst->child_religous;
		$profile_data['nationality'] = $_pst->child_nationality;
		
		// $profile_data['f_ssp_status'] = $_pst->nationality_ssp_number;
		// $profile_data['f_visa_status'] = $_pst->nationality_visa_status;
		// $profile_data['f_authorized_stay'] = $_pst->nationality_auth_stay;
		// $profile_data['f_passport_no'] = $_pst->nationality_passport_no;
		// $profile_data['f_i_card_no'] = $_pst->nationality_icard_no;
		// $profile_data['f_date_if_issue'] = $this->_time_format($_pst->nationality_date_issued);
		
		$profile_data['present_address'] = $_pst->child_city_address;
		$profile_data['contact_no'] = $_pst->child_telno;
		$profile_data['how_long_living_present_address'] = $_pst->child_adresshowlong;
		$profile_data['last_school_name'] = $_pst->school_last_attended;
		$profile_data['last_school_level'] = $_pst->school_level_completed;
		$profile_data['last_school_address'] = $_pst->school_address;
		$profile_data['fathername'] = $_pst->father_name;
		$profile_data['father_age'] = $_pst->father_age;
		$profile_data['father_nationality'] = $_pst->father_citizenship;
		$profile_data['father_educ_attain'] = $_pst->father_educ;
		$profile_data['father_hobby_talent'] = $_pst->father_talent;
		$profile_data['father_occupation'] = $_pst->father_occup;
		$profile_data['father_office_add'] = $_pst->father_office_address;
		$profile_data['father_office_num'] = $_pst->father_office_tel;
		$profile_data['mothername'] = $_pst->mother_name;
		$profile_data['mother_age'] = $_pst->mother_age;
		$profile_data['mother_nationality'] = $_pst->mother_citizenship;
		$profile_data['mother_educ_attain'] = $_pst->mother_educ;
		$profile_data['mother_hobby_talent'] = $_pst->mother_talent;
		$profile_data['mother_occupation'] = $_pst->mother_occup;
		$profile_data['mother_office_add'] = $_pst->mother_office_address;
		$profile_data['mother_office_num'] = $_pst->mother_office_tel;
		$profile_data['guardian_name'] = $_pst->guardian_name;
		$profile_data['relationship'] = $_pst->guardian_relation;
		$profile_data['guardian_address'] = $_pst->guardian_address;
		$profile_data['guardian_contact_no'] = $_pst->guardian_contact_num;
		$profile_data['guardian_reason'] = $_pst->guardian_reason;

		$this->db->set($profile_data)->where('profile_id',$p_id)->update('profiles');
		return $this->db->affected_rows() >= 1 ? TRUE : FALSE;
	}

	function _update_family_history_data($_pst,$e_id= '',$p_id = '')
	{
		$profile_data['parent_status'] = $_pst->parent_status;
		$profile_data['parent_status_how_long'] = $_pst->status_how_long;
		$profile_data['parent_rights'] = $_pst->right_over_child;
		$profile_data['child_pos'] = $_pst->position_of_child;
		$profile_data['child_adopted'] = $_pst->is_child_adopted;
		$profile_data['child_age_adopted'] = $_pst->age_of_adoption;
		$profile_data['child_aware_adopted'] = $_pst->child_aware_adopted;
		$profile_data['mother_present_pregnant'] = $_pst->mother_presently_pregnant;
		$profile_data['mother_due_date'] = $_pst->mother_pregnancy_due_date;
		$profile_data['family_deaths'] = $_pst->family_deaths;
		$profile_data['family_deaths_child_relation'] = $_pst->family_deaths_relation;
		$profile_data['family_accidents'] = $_pst->family_accidents;
		$profile_data['family_accidents_child_relation'] = $_pst->family_accidents_relation;
		$profile_data['what'] = $_pst->what;
		$profile_data['when'] = $_pst->when;
		$profile_data['sibling_age1'] = $_pst->siblingage1;
		$profile_data['sibling_age2'] = $_pst->siblingage2;
		$profile_data['sibling_age3'] = $_pst->siblingage3;
		$profile_data['sibling_age4'] = $_pst->siblingage4;
		$profile_data['sibling_age5'] = $_pst->siblingage5;
		$profile_data['sibling_age6'] = $_pst->siblingage6;
		$profile_data['sibling_fname1'] = $_pst->siblingfname1;
		$profile_data['sibling_fname2'] = $_pst->siblingfname2;
		$profile_data['sibling_fname3'] = $_pst->siblingfname3;
		$profile_data['sibling_fname4'] = $_pst->siblingfname4;
		$profile_data['sibling_fname5'] = $_pst->siblingfname5;
		$profile_data['sibling_fname6'] = $_pst->siblingfname6;
		$profile_data['sibling_lname1'] = $_pst->siblinglname1;
		$profile_data['sibling_lname2'] = $_pst->siblinglname2;
		$profile_data['sibling_lname3'] = $_pst->siblinglname3;
		$profile_data['sibling_lname4'] = $_pst->siblinglname4;
		$profile_data['sibling_lname5'] = $_pst->siblinglname5;
		$profile_data['sibling_lname6'] = $_pst->siblinglname6;
		$profile_data['other_household_member1'] = $_pst->hhmembers1;
		$profile_data['other_household_member2'] = $_pst->hhmembers2;
		$profile_data['other_household_member3'] = $_pst->hhmembers3;
		$profile_data['other_household_member4'] = $_pst->hhmembers4;
		$profile_data['other_household_member5'] = $_pst->hhmembers5;
		$profile_data['other_household_member6'] = $_pst->hhmembers6;
		$profile_data['other_household_age1'] = $_pst->hhmembersage1;
		$profile_data['other_household_age2'] = $_pst->hhmembersage2;
		$profile_data['other_household_age3'] = $_pst->hhmembersage3;
		$profile_data['other_household_age4'] = $_pst->hhmembersage4;
		$profile_data['other_household_age5'] = $_pst->hhmembersage5;
		$profile_data['other_household_age6'] = $_pst->hhmembersage6;
		$profile_data['languages_spoken_at_home'] = $_pst->language_at_home;
		$profile_data['family_activities'] = $_pst->family_activities;
		$profile_data['family_activities_frequent'] = $_pst->family_activities_frequent;
		$profile_data['time_spent_on_tv'] = $_pst->tv_time;
		$profile_data['tv_whom'] = $_pst->tv_whom;
		$profile_data['time_spent_on_radio'] = $_pst->radio_time;
		$profile_data['radio_whom'] = $_pst->radio_whom;
		$profile_data['time_spent_on_pc'] = $_pst->computergames_time;
		$profile_data['pc_whom'] = $_pst->computergames_whom;
		$profile_data['responsibility_what'] = $_pst->child_responsibilities_what;
		$profile_data['child_playgroup'] = $_pst->child_play_group;
		$profile_data['child_playgroup_frequent'] = $_pst->child_play_group_frequent;
		$profile_data['child_interest'] = $_pst->other_interest;

		$this->db->set($profile_data)->where('profile_id',$p_id)->update('profiles');
		return $this->db->affected_rows() >= 1 ? TRUE : FALSE;
	}

	function _update_developmental_history_data($_pst,$e_id= '',$p_id = '')
	{
		$profile_data['dhc_pregnancy_length']= $_pst->length_of_preg;
		$profile_data['dhc_delivery_form']= $_pst->form_of_del;
		$profile_data['dhc_compications']= $_pst->complications;
		$profile_data['dhc_sleep_time']= $_pst->go_to_bed;
		$profile_data['dhc_wakup_time']= $_pst->get_up_bed;
		$profile_data['dhc_sleeping_disturbance_what']= $_pst->sleeping_dist;
		$profile_data['dhc_child_own_room']= $_pst->own_room;
		$profile_data['dhc_room_shares_who']= $_pst->room_shares;
		$profile_data['dhc_child_wet_bed']= $_pst->wet_bed;
		$profile_data['dhc_defect_specify']= $_pst->observable_difficulties;
		$profile_data['dhc_defect_when']= $_pst->observable_difficulties_since_when;
		$profile_data['dhc_defect_actions']= $_pst->observable_difficulties_action_taken;
		$profile_data['dhc_speech_problem_what']= $_pst->speech_problems;
		$profile_data['dhc_speech_problem_when']= $_pst->speech_problems_since_when;
		$profile_data['dhc_speech_problem_actions']= $_pst->speech_problems_actions_taken;
		$profile_data['dhc_hearing_last_check']= $_pst->hearing_last_checked;
		$profile_data['dhc_hearing_problems_what']= $_pst->hearing_problems;
		$profile_data['dhc_hearing_problems_when']= $_pst->hearing_problems_since_when;
		$profile_data['dhc_hearing_problem_actions']= $_pst->hearing_problems_actions_taken;
		$profile_data['dhc_vision_checked']= $_pst->vision_last_checked;
		$profile_data['dhc_sight_problems_what']= $_pst->vision_specify;
		$profile_data['dhc_hinder_learning_process']= $_pst->hinder_childs_learning;

		$this->db->set($profile_data)->where('profile_id',$p_id)->update('profiles');
		return $this->db->affected_rows() >= 1 ? TRUE : FALSE;
	}

	function _update_childhood_experiences_data($_pst,$e_id= '',$p_id = ''){

		$profile_data['ce_trauma_exp_what'] = $_pst->traumatic_experiences;
		$profile_data['ce_trauma_exp_age'] = $_pst->age_trauma;
		$profile_data['ce_trauma_exp_reaction'] = $_pst->trauma_reaction;
		$profile_data['ce_trauma_exp_affected'] = $_pst->how_trauma_affected_child;
		$profile_data['ce_special_fears_what'] = $_pst->special_fears;
		$profile_data['ce_special_fears_when'] = $_pst->when_what_triggered_fear;
		$profile_data['ce_special_fears_handle'] = $_pst->how_do_you_handle;

		$this->db->set($profile_data)->where('profile_id',$p_id)->update('profiles');
		return $this->db->affected_rows() >= 1 ? TRUE : FALSE;
	}
	
	function _update_health_history_data($_pst,$e_id= '',$p_id = ''){

		$profile_data['hhc_past_illness'] = $_pst->past_illness;
		$profile_data['hhc_frequent_colds'] = $_pst->frequent_colds;
		$profile_data['hhc_frequent_headaches'] = $_pst->frequent_headaches;
		$profile_data['hhc_frequent_stomachaches'] = $_pst->frequent_stomachaches;
		$profile_data['hhc_dizziness'] = $_pst->frequent_dizziness;
		$profile_data['hhc_vomitting'] = $_pst->frequent_vommiting;
		$profile_data['hhc_major_operations_specify'] = $_pst->major_operations;
		$profile_data['hhc_major_injury_specify'] = $_pst->major_injury;
		$profile_data['hhc_diagnosed_specify'] = $_pst->major_ailment;
		$profile_data['hhc_medication'] = $_pst->medication_child_taking;
		$profile_data['hhc_alergic_med'] = $_pst->medication_allergy;
		$profile_data['hhc_general_eating'] = $_pst->child_eating_habbit;
		$profile_data['hhc_food_restrictions'] = $_pst->dietary_restrictions;
		$profile_data['hhc_school_help_eating'] = $_pst->school_help_eating_habbit;

		$this->db->set($profile_data)->where('profile_id',$p_id)->update('profiles');
		return $this->db->affected_rows() >= 1 ? TRUE : FALSE;
	}
	
	function _update_discipline_data($_pst,$e_id= '',$p_id = '')
	{

		$profile_data['d_method_of_discipline'] = $_pst->methods_of_discp;
		$profile_data['d_handles_discipline'] = $_pst->who_handles_discp_home;
		$profile_data['d_discipline_effective'] = $_pst->how_effective_discp;
		$profile_data['d_discipline_rod_why'] = $_pst->rod_discp;
		$profile_data['d_teacher_use_rod_why'] = $_pst->allow_teacher_use_rod;
		$profile_data['d_suggest_discipline'] = $_pst->what_form_of_discp_use;
		$profile_data['d_school_help_discipline'] = $_pst->school_supp_discp;
		$profile_data['d_provide_information'] = $_pst->other_info_about_child;

		$this->db->set($profile_data)->where('profile_id',$p_id)->update('profiles');
		return $this->db->affected_rows() >= 1 ? TRUE : FALSE;
	}

	public function get_stud_commendations($eid = '',$start,$limit)
	{
		$get_commendations = 'SELECT concat_ws("",emp.first_name," ",emp.last_name) as teacher,
									 sc.id as commend_id,
									 DATE_FORMAT(sc.created,"%M %d, %Y - %h:%i") as date_sent,
									 message as msg,
									 sc.read as is_read
							  FROM student_commendations sc
							  LEFT JOIN employees emp on sc.employee = emp.emp_id
							  WHERE sc.student_id = ?
							  AND sc.ay_id = ?
							  ORDER BY sc.read,sc.created DESC
							  LIMIT '.$start.','.$limit;
		$result = $this->db->query($get_commendations,[$eid,$this->c_sy->id]);

		return $result->num_rows() >=1 ? $result->result() : FALSE;
	}

	public function count_stud_commendations($eid)
	{
		$get_commendations = 'SELECT count(sc.id) as count
							  FROM student_commendations sc
							  WHERE sc.student_id = ?
							  AND sc.ay_id = ?';
		$result = $this->db->query($get_commendations,[$eid,$this->c_sy->id]);

		return $result->num_rows() >=1 ? $result->row()->count : FALSE;
	}

	public function read_commendation($id = '')
	{
		$this->db->set('read',1)->where('id',$id)->limit(1)->update('student_commendations');
		return $this->db->affected_rows() >= 1 ? TRUE : FALSE;
	}
}

?>
